<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="宿舍区" prop="district">
        <el-input v-model="dataForm.district" placeholder="宿舍区"></el-input>
      </el-form-item>
      <el-form-item label="宿舍人数" prop="count">
        <el-input v-model="dataForm.count" placeholder="宿舍人数"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <!-- <el-input v-model="dataForm.sex" placeholder="性别"></el-input> -->
        <el-select v-model="dataForm.sex" placeholder="请选择性别">
          <el-option
            v-for="item in options01"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <!-- <el-input v-model="dataForm.grade" placeholder="年级"></el-input> -->
        <el-select v-model="dataForm.grade" placeholder="请选择年级">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍号" prop="dormId">
        <el-input v-model="dataForm.dormId" placeholder="宿舍号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options01: [
        {
          value: 0,
          label: "男",
        },
        {
          value: 1,
          label: "女",
        }
      ],
      options: [
        {
          value: 0,
          label: "大一",
        },
        {
          value: 1,
          label: "大二",
        },
        {
          value: 2,
          label: "大三",
        },
        {
          value: 3,
          label: "大四",
        },
      ],
      visible: false,
      dataForm: {
        id: 0,
        district: "",
        count: "",
        sex: "",
        grade: "",
        dormId: "",
      },
      dataRule: {
        district: [
          { required: true, message: "宿舍区不能为空", trigger: "blur" },
        ],
        count: [
          { required: true, message: "宿舍人数不能为空", trigger: "blur" },
        ],
        sex: [
          {
            required: true,
            message: "宿舍学生性别(0:男 1：女)不能为空",
            trigger: "blur",
          },
        ],
        grade: [
          {
            required: true,
            message: "年级（0：大一 1：大二 2：大三 3：大四）不能为空",
            trigger: "blur",
          },
        ],
        dormId: [
          { required: true, message: "宿舍号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormdetail/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.district = data.dormDetail.district;
              this.dataForm.count = data.dormDetail.count;
              this.dataForm.sex = data.dormDetail.sex;
              this.dataForm.grade = data.dormDetail.grade;
              this.dataForm.dormId = data.dormDetail.dormId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormdetail/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              district: this.dataForm.district,
              count: this.dataForm.count,
              sex: this.dataForm.sex,
              grade: this.dataForm.grade,
              dormId: this.dataForm.dormId,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
